<?php
require_once 'Encode.php';
require_once 'communityModel.php';
require_once 'userModel.php';
require_once 'DBConnector.php';
require_once 'view.php';
session_start();


$request = e($_REQUEST['request']);
if($request == null){
	$request = "showCommunityCategoryList";
}
$request();

function showCommunityCategoryList(){
	$communityModel = new communityModel();
	$communityModel->setTargetTable(CommunityModel::TABLE_NAME_COMMUNITY);
	$sth = DBConnector::getDb()->prepare("select * from tCommunity");
	$data = $communityModel->select($sth);
	require_once 'view.php';
	$view = new View($data);
	$view->render('communityList.php');
}

function showJoinedCommunityBoardArticle(){
	$cIndex = e($_REQUEST['CIndex']);
	$communityModel = new communityModel();
	$communityModel->setTargetTable(CommunityModel::TABLE_NAME_COMMUNITY_BOARD);
	$sth = DBConnector::getDb()->prepare("select * from tCommunityBoard where CIndex=".$cIndex);
	$data['article'] = $communityModel->select($sth);

	$data['CIndex'] = e($_REQUEST['CIndex']);

	require_once 'view.php';
	$view = new View($data);
	$view->render('communityArticleList.php');
}

function showUpdateJoinedCommunityBoardArticle(){
	$cbIndex = e($_REQUEST['CBIndex']);
	$communityModel = new communityModel();
	$communityModel->setTargetTable(CommunityModel::TABLE_NAME_COMMUNITY_BOARD);
	$sth = DBConnector::getDb()->prepare("select * from tCommunityBoard where CBIndex=".$cbIndex);
	$data = $communityModel->select($sth);
	$view = new View($data[0]);
	$view->render('communityArticleEdit.php');
}

function updateJoinedCommunityBoardArticle(){
	$cbTitle = e($_REQUEST['CBtitle']);
	$cbContent = e($_REQUEST['CBContent']);
	$cbIndex = e($_REQUEST['CBIndex']);
	$communityModel = new communityModel();
	$communityModel->setTargetTable(CommunityModel::TABLE_NAME_COMMUNITY_BOARD);
	$communityModel-> update($cbTitle,$cbContent,$cbIndex);


	$sth = DBConnector::getDb()->prepare("select * from tCommunityBoard where CBIndex=".$cbIndex);
	$data = $communityModel->select($sth);
	$data = $data[0];

	$communityModel->setTargetTable(CommunityModel::TABLE_NAME_COMMUNITY_BOARD_RESPONSE);
	$sth = DBConnector::getDb()->prepare("select * from tCommunityBoardReponse where CBIndex=".$cbIndex);
	$data1 = $communityModel->select($sth);
	$data['comment'] = $data1;

	$communityModel->setTargetTable(CommunityModel::TABLE_NAME_COMMUNITY);
	$sth = DBConnector::getDb()->prepare("select * from tCommunity where CIndex=".e($_REQUEST['CIndex']));
	$data2 = $communityModel->select($sth);
	$data['community'] = $data2[0];

	$view = new View($data);
	$view->render('communityArticle.php');
}


function deleteJoinedCommunityBoardArticle(){
	$communityModel = new communityModel();
	$communityModel->setTargetTable(CommunityModel::TABLE_NAME_COMMUNITY_BOARD);
	$communityModel->delete(e($_REQUEST['CBIndex']));

	$sth = DBConnector::getDb()->prepare("select * from tCommunityBoard where CIndex=".e($_REQUEST['CIndex']));
	$data['article'] = $communityModel->select($sth);
	$data['CIndex'] = e($_REQUEST['CIndex']);
	$view = new View($data);
	$view->render('communityArticleList.php');
}

function deleteCommunityBoardResponse(){
	$communityModel = new communityModel();
	$communityModel->setTargetTable(CommunityModel::TABLE_NAME_COMMUNITY_BOARD_RESPONSE);
	$communityModel->delete(e($_REQUEST['CBRIndex']));

	$communityModel->setTargetTable(CommunityModel::TABLE_NAME_COMMUNITY_BOARD);
	$sth = DBConnector::getDb()->prepare("select * from tCommunityBoard where CBIndex=".e($_REQUEST['CBIndex']));
	$data = $communityModel->select($sth);
	$data = $data[0];

	$communityModel->setTargetTable(CommunityModel::TABLE_NAME_COMMUNITY_BOARD_RESPONSE);
	$sth = DBConnector::getDb()->prepare("select * from tCommunityBoardReponse where CBIndex=".e($_REQUEST['CBIndex']));
	$data1 = $communityModel->select($sth);
	$data['comment'] = $data1;

	$communityModel->setTargetTable(CommunityModel::TABLE_NAME_COMMUNITY);
	$sth = DBConnector::getDb()->prepare("select * from tCommunity where CIndex=".e($_REQUEST['CIndex']));
	$data2 = $communityModel->select($sth);
	$data['community'] = $data2[0];
	$view = new View($data);
	$view->render('communityArticle.php');
}

function insertMainBoardArticleReponse(){
	$communityModel = new communityModel();
	$communityModel->setTargetTable(communityModel::TABLE_NAME_COMMUNITY_BOARD_RESPONSE);
	$communityModel->insert(e($_REQUEST["CBIndex"]),e($_REQUEST["userId"]),e($_REQUEST["CBRContent"]));

	$communityModel->setTargetTable(CommunityModel::TABLE_NAME_COMMUNITY_BOARD);
	$sth = DBConnector::getDb()->prepare("select * from tCommunityBoard where CBIndex=".e($_REQUEST['CBIndex']));
	$data = $communityModel->select($sth);
	$data = $data[0];

	$communityModel->setTargetTable(CommunityModel::TABLE_NAME_COMMUNITY_BOARD_RESPONSE);
	$sth = DBConnector::getDb()->prepare("select * from tCommunityBoardReponse where CBIndex=".e($_REQUEST['CBIndex']));
	$data1 = $communityModel->select($sth);
	$data['comment'] = $data1;

	$communityModel->setTargetTable(CommunityModel::TABLE_NAME_COMMUNITY);
	$sth = DBConnector::getDb()->prepare("select * from tCommunity where CIndex=".e($_REQUEST['CIndex']));
	$data2 = $communityModel->select($sth);
	$data['community'] = $data2[0];

	$view = new View($data);
	$view->render('communityArticle.php');
}


function showCommunityArticle(){
	$communityModel = new communityModel();

	$communityModel->setTargetTable(CommunityModel::TABLE_NAME_COMMUNITY_BOARD);
	$sth = DBConnector::getDb()->prepare("select * from tCommunityBoard where CBIndex=".e($_REQUEST['CBIndex']));
	$data = $communityModel->select($sth);
	$data = $data[0];

	$communityModel->setTargetTable(CommunityModel::TABLE_NAME_COMMUNITY_BOARD_RESPONSE);
	//$sth = DBConnector::getDb()->prepare("select * from tCommunityBoardReponse where CBIndex=".e($_REQUEST['CBIndex']));
	$sth = DBConnector::getDb()->prepare("select * from tCommunityBoardReponse join tUser on tCommunityBoardReponse.userId =tUser.userId  where CBIndex=".e($_REQUEST['CBIndex']));
	$data1 = $communityModel->select($sth);
	$data['comment'] = $data1;
	//var_dump($data['comment']);
	$communityModel->setTargetTable(CommunityModel::TABLE_NAME_COMMUNITY);
	$sth = DBConnector::getDb()->prepare("select * from tCommunity where CIndex=".e($_REQUEST['CIndex']));
	$data2 = $communityModel->select($sth);
	$data['community'] = $data2[0];

	$view = new View($data);
	$view->render('communityArticle.php');
	}

function showAddCommunityArticle(){
	$data['CIndex'] = e($_REQUEST['CIndex']);
	require_once 'view.php';
	$view = new View($data);
	//var_dump($data);
	$view->render('addCommunityArticle.php');

}

function insertJoinedCommunityBoardArticle(){
	$communityModel = new communityModel();
	$communityModel->setTargetTable(communityModel::TABLE_NAME_COMMUNITY_BOARD);
	$communityModel->insert(e($_REQUEST["CIndex"]),e($_REQUEST["userId"]),e($_REQUEST["CBTitle"]),e($_REQUEST["CBContent"]));

	$sth = DBConnector::getDb()->prepare("select * from tCommunityBoard where CIndex=".e($_REQUEST['CIndex']));
	$data['article'] = $communityModel->select($sth);
	$data['CIndex'] = e($_REQUEST['CIndex']);
	$view = new View($data);
	$view->render('communityArticleList.php');
}

function showCommunityMember(){
	$communityModel = new communityModel();
	$communityModel->setTargetTable(CommunityModel::TABLE_NAME_COMMUNITY_CATEGORY);
	$sth = DBConnector::getDb()->prepare("select userId from tCommunityCategory where CIndex=".e($_REQUEST['CIndex']));
	$data = $communityModel->select($sth);

	$userModel = new UserModel();
	$db = DBConnector::getDB();

	$sql = "select * from tUser where userId in (";
	for ($i=0 ; $i < count($data);$i++){
		$sql = $sql."'".$data[$i]['userId']."'";
		if($i < count($data)-1){
			$sql = $sql.",";
		}
	}
	$sql = $sql.")";
	$sth = $db->prepare($sql);
	$result = $userModel->select($sth);
	$result['showCommunity'] = true;
	$result['CIndex'] = e($_REQUEST['CIndex']);
	$view = new View($result);
	$view->render("profileList.php");
}

function joinCommunnity(){
	$communityModel = new communityModel();
	$communityModel->setTargetTable(CommunityModel::TABLE_NAME_COMMUNITY_CATEGORY);
	$communityModel->insert($_SESSION['userId'],e($_REQUEST['CIndex']));
	showCommunityDetall();

}

function showCommunityDetall(){
	$communityModel = new communityModel();
	$communityModel->setTargetTable(CommunityModel::TABLE_NAME_COMMUNITY);
	$sth = DBConnector::getDb()->prepare("select * from tCommunity where CIndex=".e($_REQUEST['CIndex']));
	$data = $communityModel->select($sth);

	$communityModel->setTargetTable(CommunityModel::TABLE_NAME_COMMUNITY_CATEGORY);
	$sth = DBConnector::getDb()->prepare("select * from tCommunityCategory where CIndex=".e($_REQUEST['CIndex'])." and userId = '".$_SESSION['userId']."'");
	$data1 = $communityModel->select($sth);
	$data[0]['join'] = $data1;

	$view = new View($data[0]);
	$view->render('community.php');

}
